<template>
  <div class="discount-container">
      <template v-if="showSkeleton">
          <s-discount ></s-discount>
      </template>
      <template v-else>
          <ul v-infinite-scroll="requestData" infinite-scroll-disabled="cellLoading" infinite-scroll-distance="20" infinite-scroll-immediate-check="false">
              <router-link tag="li" v-for="item in itemArr" :to=" 'details/' + item.id" :key="item.id">
                <v-card :data="item"></v-card>
              </router-link>
          </ul>
      </template>
  </div>
</template>

<script>
import { mapState } from "vuex";
import vCard from "./card.vue";
import Vue from "vue";
import { InfiniteScroll } from "mint-ui";
Vue.use(InfiniteScroll);
import sDiscount from "../../skeletons/sDiscount.vue";

export default {
  name: "discount-combine",
  data() {
    return {
      itemArr: [],
      cellLoading: true,
      curPage: 0,
      pageNum: 10,
      existIdArr: [],
      showSkeleton:true
    };
  },
  components: { vCard,sDiscount },
  activated() {},
  mounted() {
    this.requestData(false);
  },
  methods: {
    requestData(infinite = true) {
      if(this.$route.query&&this.$route.query.tabIndex!=1) return;
      this.$store.dispatch("toggleLoading", true);
      this.$http
        .post(this.path + "store/Goods/goods_list", {
          type: "2",
          page: this.curPage,
          storeid: this.storeid
        })
        .then(response => {
          this.$store.dispatch("toggleLoading", false);
          if (infinite && this.curPage == 0) return;
          if (infinite && this.cellLoading) return;
          this.cellLoading = true;
          if (this.showSkeleton) this.showSkeleton=false;
          console.log(response.body.result.arr);
          let serverData = response.body.result.arr;
          if (!serverData || serverData.length == 0) {
            //数据为空
            this.cellLoading = true;
          } else {
            //有数据
            if (serverData.length % 10 == 0) {
              //满一页数据
              this.curPage++;
              this.cellLoading = false;
            } else {
              //不满一页
              this.cellLoading = true;
            }
            let detailsArr = this.analysisData(serverData);
            for (let i = 0; i < detailsArr.length; i++) {
              let orderid = detailsArr[i].id;
              if (this.existIdArr.indexOf(orderid) >= 0) continue;
              this.itemArr.push(detailsArr[i]);
              this.existIdArr.push(orderid);
            }
          }
        })
        .catch(response => {
          console.log(response);
          this.$store.dispatch("toggleLoading", false);
        });
    },
    analysisData(data) {
      let detailsArr = [];
      data.forEach((val, index, arr) => {
        detailsArr.push({
          id: val.id,
          imgUrl: val.icon,
          itemName: val.gname,
          dec:val.message,
          price: (parseFloat(val.price)*parseFloat(val.discount)).toFixed(2)
        });
      });
      return detailsArr;
    }
  },
  computed: {
    ...mapState(["openid", "path", "userid", "storeid",'soursePath'])
  }
};
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
.discount-container {
  font-family: 'Microsoft YaHei';
  width: 100%;
  flex: 1;
  background-color: #efeff0;

  ul {
    padding: 0.09rem 0.09rem 0 0.09rem;

    li {
      width: 100%;
      margin-bottom: 0.09rem;
      // box-shadow: 0 1px 0px rgba(150, 150, 150, 0.1);
    }
  }

  
}
</style>
